package com.sskj.bibi.presenter;

import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.bibi.bean.EntrustBean;
import com.sskj.bibi.ui.fragment.AllEntrustFragment;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.http.CallBackOption;
import com.sskj.lib.http.JsonConverter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

public class AllEntrustPresenter extends BasePresenter<AllEntrustFragment> {
    public Flowable<List<EntrustBean>> getEntrustList(String page, String size, String code, String status, String type, UserData userData) {
        if (userData == null) {
            return Flowable.fromIterable(new ArrayList<EntrustBean>()).toList().toFlowable();
        }
        return httpService.getRecordAll(page, size, code, status, type)
                .converter(new JsonConverter<HttpData<PageBean<EntrustBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(pageBeanHttpData -> pageBeanHttpData.getData().getList())
                .onErrorReturnItem(new ArrayList<>());

    }


    public void cancelEntrust(String orderId, String tradeType) {
        httpService.cancelCoinOrder(orderId, tradeType)
                .execute(new CallBackOption<HttpData>() {
                }.loadBind(mView)
                        .execute(httpData -> {
                            ToastUtil.showShort(httpData.getMsg());
                            mView.cancelReturn();
                        }));
    }

}
